﻿using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Web.Mvc;
using Utils.Base;
using Utils.Share;

namespace Utils.EasyUI.Comboxs
{
    /// <summary>
    ///     实体组合框
    /// </summary>
    /// <typeparam name="TEntity">实体类型</typeparam>
    /// <typeparam name="TProperty">属性类型</typeparam>
    public class EntityCombox<TEntity, TProperty> : Combox<ICombox>, ICombox
    {
        /// <summary>
        ///     属性表达式
        /// </summary>
        private readonly Expression<Func<TEntity, TProperty>> _expression;

        /// <summary>
        ///     成员
        /// </summary>
        private readonly MemberInfo _memberInfo;

        /// <summary>
        ///     值
        /// </summary>
        private readonly object _value;

        /// <summary>
        ///     初始化实体组合框
        /// </summary>
        /// <param name="expression">属性表达式</param>
        public EntityCombox(Expression<Func<TEntity, TProperty>> expression)
            : this(expression, null)
        {
        }

        /// <summary>
        ///     初始化实体组合框
        /// </summary>
        /// <param name="expression">属性表达式</param>
        /// <param name="helper">HtmlHelper</param>
        public EntityCombox(Expression<Func<TEntity, TProperty>> expression, HtmlHelper<TEntity> helper)
        {
            if (helper != null)
                _value = helper.Value(expression);
            _expression = expression;
            _memberInfo = Lamanda.GetMember(_expression);
            Init();
            ExpressionResolver<ICombox, TEntity, TProperty>.Resolve(this, expression);
        }

        /// <summary>
        ///     初始化
        /// </summary>
        private void Init()
        {
            InitName();
            InitValue();
            InitType();
        }

        /// <summary>
        ///     初始化name属性
        /// </summary>
        private void InitName()
        {
            Name(Lamanda.GetName(_expression));
        }

        /// <summary>
        ///     初始值value属性
        /// </summary>
        private void InitValue()
        {
            Select(_value.ToStr());
        }

        /// <summary>
        ///     根据类型初始化控件
        /// </summary>
        private void InitType()
        {
            if (Reflection.IsBool(_memberInfo))
            {
                Bool();
                return;
            }
            if (Reflection.IsEnum(_memberInfo))
            {
                InitEnum();
            }
        }

        /// <summary>
        ///     初始化枚举
        /// </summary>
        private void InitEnum()
        {
            Enum<TProperty>();
            if (_value.ToStr().IsEmpty())
                return;

            var tt = Nullable.GetUnderlyingType(typeof (TProperty)) ?? typeof (TProperty);

            var value = _value.ToStr();
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentNullException("member");
            Select((int) System.Enum.Parse(tt, _value.ToString(), true));
        }
    }
}